<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 堆栈判断表达式是否合法
        let expression = '((1+2)*2)+(1-2)*2';
        function stackJudgeExpressionIsLegal(str) {
            let arr = [];
            let left = '(';
            let right = ')';
            for (let i = 0; i < str.length; i++) {
                if (left.includes(str[i])) {

                }
            }
        }
        stackJudgeExpressionIsLegal(expression)

        // ==============================================
        let expression2 = '3[abc]';
        // expression2 = '3[2[a]2[b]]';
        expression2 = '2[1[a]3[b]2[3[c]4[d]]]';

        function stackConvert(str) {
            let numberArr = [];
            let chatArr = [''];
            let number;
            let chat = '';
            Array.from(str).forEach(item => {
                console.log("==========:", item);
                // 做括号，添加一个空字符串
                if (item == '[') {
                    chatArr.push('');
                } else if (item == ']') {
                    number = Number(numberArr.pop());
                    chat = chatArr.pop();
                    chatArr[chatArr.length - 1] = chatArr[chatArr.length - 1] + chat.repeat(number)
                    // for (let i = 0; i < parseInt(number); i++) {
                    //     chatArr[chatArr.length - 1] = chatArr[chatArr.length - 1] + chat;
                    // }
                } else if (item.match(/\d/g)) {  // 数字
                    numberArr.push(item)
                } else {  // 字符
                    chatArr[chatArr.length - 1] = chatArr[chatArr.length - 1] + item;
                }
                console.log("numberArr:", numberArr);
                console.log("chatArr:", chatArr);
            });
            return chatArr;
        }
        // console.log("stackConvert:", stackConvert(expression2));

        expression2 = '3[2[abc]2[d]]';
        function smartRepeat(str) {
            let index = 0;
            // 数字栈
            let stack1 = [],
                // 字符栈
                stack2 = [];
            while (index < str.length - 1) {
                let string = str.slice(index);
                // 如果是数字+[
                if (string.match(/^\d+\[/)) {
                    let temp = string.match(/^(\d+)\[/)[1];
                    stack1.push(temp);
                    stack2.push('');
                    index = index + temp.length + 1;
                } else if (string.match(/^\w+\]/)) // 匹配到字符
                {
                    let temp = string.match(/^(\w+)\]/)[1];
                    stack2[stack2.length - 1] = temp;
                    index = index + temp.length;
                } else if (string[0] == ']') {
                    let number = Number(stack1.pop());
                    let chat = stack2.pop();
                    stack2[stack2.length - 1] += chat.repeat(number);
                    index++;
                } else {
                    index++;
                }
                console.log(index, string, stack1, stack2);
            }
            return stack2[0].repeat(stack1[0])
        }

        console.log("smartRepeat:", smartRepeat(expression2));
    </script>
</body>

</html>